﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.ApllicationDto;

namespace Zocono.WCS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RouteDetailInfoController : ControllerBase
    {
        private readonly ILogUtil _LogUtil;
        private readonly RouteDetailInfoApp _RouteDetailInfoApp;

        public RouteDetailInfoController(ILogUtil logUtil, RouteDetailInfoApp routeDetailInfoApp)
        {
            _LogUtil = logUtil;
            _RouteDetailInfoApp = routeDetailInfoApp;
        }

        [HttpPost]
        public async Task<HeaderResult<List<RouteDetailInfoDto>>> GetPagedRouteDetailInfoAsync(QueryRouteDetailInfoDto queryRouteDetailInfo)
        {
            HeaderResult<List<RouteDetailInfoDto>> headerResult = new HeaderResult<List<RouteDetailInfoDto>>();
            try
            {
                PageDto<RouteDetailInfoDto> pageDto = await _RouteDetailInfoApp.GetPagedRouteDetailInfoAsync(queryRouteDetailInfo);
                headerResult.IsSucceed = true;
                headerResult.Result = pageDto.Result;
                headerResult.Total = pageDto.Total;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpGet]
        public async Task<HeaderResult<List<RouteDetailInfo>>> GetRouteDetailInfoByRouteIDAsync(long routeId)
        {
            HeaderResult<List<RouteDetailInfo>> headerResult = new HeaderResult<List<RouteDetailInfo>>();
            try
            {
                List<RouteDetailInfo> list = await _RouteDetailInfoApp.GetRouteDetailInfoByRouteIDAsync(routeId);
                headerResult.IsSucceed = true;
                headerResult.Result = list;
                headerResult.Total = list.Count;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpGet]
        public async Task<HeaderResult<RouteDetailInfo>> GetRouteDetailInfoByIdAsync(long Id)
        {
            HeaderResult<RouteDetailInfo> headerResult = new HeaderResult<RouteDetailInfo>();
            try
            {
                RouteDetailInfo list = await _RouteDetailInfoApp.GetRouteDetailInfoByIdAsync(Id);
                headerResult.IsSucceed = true;
                headerResult.Result = list;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }


        [HttpGet]
        public async Task<HeaderResult<List<RouteDetailInfo>>> GetRouteDetailInfoByDetailIDAsync(long routeDetailInfoId)
        {
            HeaderResult<List<RouteDetailInfo>> headerResult = new HeaderResult<List<RouteDetailInfo>>();
            try
            {
                List<RouteDetailInfo> list = await _RouteDetailInfoApp.GetRouteDetailInfoByDetailIDAsync(routeDetailInfoId);
                headerResult.IsSucceed = true;
                headerResult.Result = list;
                headerResult.Total = list.Count;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> CreateRouteDetailInfoAsync(RouteDetailInfoDto routeDetailInfo)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _RouteDetailInfoApp.CreateRouteDetailInfoAsync(routeDetailInfo);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "新增成功" : "新增失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> ModifyRouteDetailInfoAsync(RouteDetailInfo routeDetailInfo)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _RouteDetailInfoApp.ModifyRouteDetailInfoAsync(routeDetailInfo);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "修改成功" : "修改失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> DeleteRouteDetailInfoAsync(List<RouteDetailInfo> routeDetailInfoList)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _RouteDetailInfoApp.DeleteRouteDetailInfoAsync(routeDetailInfoList);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "删除成功" : "删除失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }
    }
}
